package cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.flow.business;

import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.ListBlockRequest;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.entity.ScheduleJob;
import cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.domain.ScheduleJobContext;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import com.alibaba.fastjson.JSONArray;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;

import java.util.List;
import java.util.concurrent.ScheduledFuture;

public class EnableScheduleJobFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        flowContext.executeFlowList(new GetScheduleJobContextListFlow());
        List<ScheduleJobContext> list = flowContext.getData("list", List.class);
        if(null==list||list.isEmpty()){
            throw new IllegalArgumentException("当前定时任务流程列表为空");
        }
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        FlowMonitorOption flowMonitorOption = applicationContext.getBean(FlowMonitorOption.class);
        ThreadPoolTaskScheduler threadPoolTaskScheduler = applicationContext.getBean(ThreadPoolTaskScheduler.class);

        ListBlockRequest listBlockRequest = flowContext.checkInstanceData(ListBlockRequest.class);

        JSONArray array = new JSONArray();
        if(null!=listBlockRequest.array){
            array.addAll(listBlockRequest.array);
        }else{
            array.add(listBlockRequest.row);
        }
        List<ScheduleJob> scheduleJobList = array.toJavaList(ScheduleJob.class);
        for(ScheduleJob scheduleJob:scheduleJobList){
            ScheduleJobContext scheduleJobContext = QueryInstanceList.newQuery(list)
                    .addQuery("categoryName", scheduleJob.getCategoryName())
                    .addQuery("name", scheduleJob.getName())
                    .execute()
                    .getOne();
            if(null==scheduleJobContext){
                throw new IllegalArgumentException("定时任务流程不存在!分类:"+scheduleJob.getCategoryName()+",名称:"+scheduleJob.getName());
            }
            if(null!=scheduleJobContext.scheduledFuture&&!scheduleJobContext.scheduledFuture.isCancelled()){
                boolean cancel = scheduleJobContext.scheduledFuture.cancel(true);
                if(!cancel){
                    throw new IllegalArgumentException("定时任务还在运行!取消定时任务失败!");
                }
            }
            ScheduledFuture scheduledFuture = threadPoolTaskScheduler.schedule(new Runnable() {
                @Override
                public void run() {
                    flowMonitorOption.scheduleQuickFlow.startFlow(scheduleJobContext.scheduleJobFlow).execute();
                }
            }, new CronTrigger(scheduleJob.getCron()));
            scheduleJobContext.scheduledFuture = scheduledFuture;

            flowMonitorOption.dao.query(ScheduleJob.class)
                    .addQuery("id", scheduleJob.getId())
                    .addUpdate("enable", true)
                    .execute()
                    .update();
        }
    }

    @Override
    public String name() {
        return "启用定时任务";
    }

}
